package com.ljc.dao;

import com.ljc.entity.CollegeUsersSum;
import com.ljc.entity.User;
import com.ljc.vo.UserSearch;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import java.util.List;
@Mapper
@Repository
public interface UserDao {
    //查询该用户创建的所有用户
    List<User> findByDeanId(int deanId);
    User login(User user);
//    查询每个学院的用户数目
    @Select("select  c.id,c.name,count(*) sum " +
            "from user u inner join college c " +
            "on u.college_id=c.id  " +
            "where u.dean_id=#{deanId} and u.deleted!=1 " +
            "GROUP BY u.college_id")
    List<CollegeUsersSum> getCollegeUserSum(int deanId);
//    搜索用户
    List<User> searchUser(UserSearch userSearch);

    //根据用户名查询用户
    @Select("select * from user where name=#{name}")
    User getUserByName(User user);

    //新增用户
    @Insert("insert into user(name,password,phone,email,status,college_id,dean_id) " +
            "values(#{name},#{password},#{phone},#{email},#{status},#{collegeId},#{deanId})")
    void addUser(User user);

    //获取一个用户信息
    User getOne(int userId);

    //修改用户信息
    @Update("update user \n" +
            "set name=#{name},password=#{password},phone=#{phone},email=#{email},status=#{status} ,college_id=#{collegeId}\n" +
            "where id=#{id}")
    void updateUser(User user);
    //删除用户

    @Update("update user set deleted=1 where id=#{userId}")
    void deleteUser(int userId);

    @Select("select  c.name from user u left join college c on u.college_id=c.id " +
            "where u.id=#{id} ")
    String getCollegeNameByUserId(int id);

    //删除一个学院的用户
    @Update("update user set deleted=1 where  college_id=#{cid}")
    void deletedByCollegeId(int cid);
}
